package projet_v1.simulation.events;

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import projet_v1.Breed;
import projet_v1.Character;
import projet_v1.Item;
import projet_v1.items.ammo.Ammo;
import projet_v1.items.ammo.CategoryAmmo;
import projet_v1.items.help.FirstAidKit;
import projet_v1.items.help.Food;
import projet_v1.items.weapons.Bazooka;
import projet_v1.items.weapons.CategoryWeapon;
import projet_v1.items.weapons.LiquidNitrogen;
import projet_v1.items.weapons.Shotgun;
import projet_v1.items.weapons.WoodenStake;
import projet_v1.simulation.Field;
import projet_v1.simulation.Location;
/**
 * A rescue event that add some items needed by the humans (like ammo, weapons, food...)
 * 
 * @author Romain Roufast
 * 
 */
public class Rescue extends Event {

	private static final int TIME = 2500;
	private static final int DIMENSION = 1;

	private static final double PROBA_WEAPON = 0.8;
	private static final double PROBA_HEALTH = 0.3;
	private static final double PROBA_AMMO = 0.1;
	private static final double PROBA_FOOD = 0.3;

	public Rescue(Location l) {
		super(l, DIMENSION, Event.IS_GOOD_FOR_HUMANITY);
	}
	
	/**
	 * Lay down some items to help the humans
	 */
	@Override
	public boolean happend(List<Character> characters, Field field) {
		boolean stillHasEffect = false;
		Random rand = new Random();

		List<Location> l = new ArrayList<Location>();
		l = field.adjacentLocations(startLocation, dimension);
		l.add(startLocation);

		for (Location location : l) {
			if (rand.nextDouble() <= PROBA_WEAPON) {
				field.placeItem(rescueWithWeapon(rand), location);
			} else if (rand.nextDouble() <= PROBA_AMMO) {
				field.placeItem(rescueWithAmmo(rand), location);
			} else if (rand.nextDouble() <= PROBA_HEALTH) {
				field.placeItem(rescueWithHealthItems(), location);
			} else {
				field.placeItem(rescueWithFood(), location);
			}
		}

		if ((TIME - duration) > 0) {
			duration++;
			stillHasEffect = true;
		}

		return stillHasEffect;
	}

	
	private Item rescueWithAmmo(Random rand) {

		CategoryAmmo[] cat = CategoryAmmo.values();
		Item i = null;
		int index = rand.nextInt(cat.length);
		i = new Ammo(cat[index]);

		return i;
	}

	private Item rescueWithWeapon(Random rand) {

		CategoryWeapon[] cat = CategoryWeapon.values();
		Item i = null;
		int index = rand.nextInt(cat.length);

		switch (cat[index]) {
		case SHOTGUN:
			i = new Shotgun();
			break;
		case WOODENSTAKE:
			i = new WoodenStake();
			break;
		case BAZOOKA:
			i = new Bazooka();
			break;
		case LIQUIDNITRO:
			i = new LiquidNitrogen();
			break;
		}
		return i;
	}

	private Item rescueWithHealthItems() {

		Item i = new FirstAidKit();
		return i;
	}

	private Item rescueWithFood() {

		Item i = new Food();
		return i;
	}

}
